<template>
  <button 
    class="vote-button"
    @click.stop="handleVote"
  >
    <span class="vote-icon">👍</span>
    <span class="vote-text">投&nbsp;票</span>
  </button>
</template>

<script setup lang="ts">
import { isAuthenticated } from '../../services/userService'
import toastService from '../../services/toastService'

// 定义组件的属性
const props = withDefaults(defineProps<{
  animeId: number
}>(), {})

// 定义组件的事件
const emit = defineEmits<{
  vote: [animeId: number]
}>()

// 处理投票逻辑
const handleVote = async () => {
  // 检查用户是否已登录
  if (!isAuthenticated()) {
    // 显示登录提示
    if ((window as any).showLoginModal) {
      (window as any).showLoginModal('请先登录后再投票！')
    } else if ((window as any).showLoginPrompt) {
      (window as any).showLoginPrompt()
    } else {
      toastService.info('请先登录后再投票！')
    }
    return
  }
  
  // 直接触发投票事件，由父组件统一处理API调用
  emit('vote', props.animeId)
}
</script>

<style scoped>
.vote-button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.2s ease;
  font-weight: 500;
}

.vote-button:hover:not(:disabled) {
  background-color: #45a049;
}

.vote-button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
  opacity: 0.7;
}

.vote-icon {
  font-size: 16px;
}

.vote-text {
  font-size: 1em;
  font-weight: 500;
  letter-spacing: 0.5px;
}
</style>